﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace syc
{

    public class PageExport : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.ServerVariables["REQUEST_METHOD"].ToLower() != "post")
            {
                context.Response.End();
            }
            context.Response.Charset = "utf-8";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            if (context.Request.Form["filename"].EndsWith(".xls"))
            {
                context.Response.ContentType = "application/ms-excel";
            }
            else
            {
                context.Response.ContentType = "application/ms-word";
            }
            context.Response.AddHeader("content-disposition", "attachment; filename=\"" + context.Server.UrlEncode(context.Request.Form["filename"]) + "\"");

            context.Response.Write(
@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html>
<head>
<meta http-equiv=""Content-Language"" content=""zh-cn""/>
<meta http-equiv=""Content-Type"" content="""
            + context.Response.ContentType + "; charset=utf-8\"/></head><body>");
            context.Response.Write(context.Request.Form["content"]);
            context.Response.Write("</body></html>");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
